package com.ca.project.Server.Modules.RepairImg.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ca.project.Server.Modules.RepairImg.Context.UploadRepairImgContext;
import com.ca.project.Server.Modules.RepairImg.Converter.RepairImgConverter;
import com.ca.project.Server.Modules.RepairImg.Entity.RepairImg;
import com.ca.project.Server.Modules.RepairImg.PO.UploadRepairImgPO;
import com.ca.project.Server.Modules.RepairImg.Service.RepairImgService;
import com.ca.project.Server.Modules.RepairImg.VO.DeleteRepairImgVO;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @PACKAGE_NAME: com.ca.project.Server.Modules.RepairImg.Controller
 * @NAME: RepairImgController
 * @USER: ASUS
 * @DATE: 4/1/2025
 * @TIME: 下午6:07
 **/
@RestController
@RequestMapping("/repair_img")
@Api(tags = "维修订单图片模块")
public class RepairImgController {

    @Autowired
    private RepairImgService repairImgService;
    @Autowired
    private RepairImgConverter repairImgConverter;

    @ApiOperation(
            value = "维修订单图片上传接口",
            notes = "该接口提供了维修订单图片上传的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/upload")
    public R ImgUpload(@Validated UploadRepairImgPO uploadRepairImgPO) {
        UploadRepairImgContext uploadRepairImgContext =
                repairImgConverter.UploadRepairImgPO2UploadRepairImgContext(uploadRepairImgPO);
        String url = repairImgService.upload(uploadRepairImgContext);
        return R.data(url);
    }

    @ApiOperation(
            value = "维修订单图片删除接口",
            notes = "该接口提供了维修订单图片删除的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/delete")
    public R ImgDelete(@Validated @RequestBody DeleteRepairImgVO deleteRepairImgVO){
        String url = deleteRepairImgVO.getR_img_url();
        repairImgService.deleteRepairImg(url);
        return R.success();
    }

    @ApiOperation(
            value = "查询所有图片信息-分页",
            notes = "该接口提供了查询所有图片信息-分页的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/{currentPage}/{PageSize}")
    public R getPage(@PathVariable Integer currentPage, @PathVariable Integer PageSize, RepairImg repairImg){
        IPage<RepairImg> page = repairImgService.getPage(currentPage,PageSize,repairImg);
        if (currentPage>page.getPages())
            page = repairImgService.getPage((int) page.getPages(),PageSize);
        return R.data(page);
    }
}
